package cn.tangwei.org.controller;

import cn.tangwei.basic.utils.AjaxRusult;
import cn.tangwei.basic.utils.PageList;
import cn.tangwei.org.domain.PetTypes;
import cn.tangwei.org.query.PetTypesQuery;
import cn.tangwei.org.service.IPetTypesService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pets")
@Api(tags = "宠物类型接口",description = "宠物类型接口详细描述")
public class PetTypesContorller {
    @Autowired
    private IPetTypesService petTypesService;

    /**
     * 添加和修改
     * @param petType
     * @return
     */
    @PutMapping
    public AjaxRusult addOrUpdate(@RequestBody PetTypes petType){
        try {
            if(petType.getId() == null){
                petTypesService.add(petType);
            }else {
                petTypesService.update(petType);
            }
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxRusult delete(@PathVariable("id") Long id){
        try {
            petTypesService.delete(id);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }

    /**
     * 查询一条
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public PetTypes queryById(@PathVariable("id") Long id){
        return petTypesService.queryById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public List<PetTypes> queryAll(){
        return petTypesService.queryAll();
    }

    /**
     * 分页加高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    public PageList<PetTypes> queryPage(@RequestBody PetTypesQuery query){
        return petTypesService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxRusult patchDel(@RequestBody List<Long> ids){
        try {
            petTypesService.patchDelete(ids);
            return AjaxRusult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxRusult.me().setMessage("操作失败");
        }
    }
}
